import { _decorator, Component, EventTouch, log, math, Node, UITransform, v3, Vec3, view } from 'cc';
import GameInfo from '../DataCenter/GameInfo';
import PlayerCenter from '../DataCenter/PlayerCenter';
import ObjPool from '../DataCenter/ObjPool';
import { RoleBase } from '../Item/Role/RoleBase';
import { GlobalContext } from '../DataCenter/GlobalContext';
import { GameController } from '../GameController';
const { ccclass, property } = _decorator;

@ccclass('BattleView')
export class BattleView extends Component {
    @property(Node)
    mask: Node = null;
    @property(Node)
    rolesContent: Node = null;
    @property(Node)
    mapNode: Node = null;
    @property(Node)
    touch: Node = null;
    @property(Node)
    battleScene: Node = null;
    @property(Node)
    startBtn: Node = null;
    @property(Node)
    quitBtn: Node = null;

    touch_bg: Node = null;
    isTouchMove: boolean = false;
    onLoad() {
        let size: math.Size = view.getVisibleSize();
        let mT = this.mask.getComponent(UITransform);
        mT.height = size.height/2 + GlobalContext.TileSize/2;
        this.mask.position = v3(0, size.height / 2,0);

        this.touch_bg = this.touch.getChildByName('bg');
        this.quitBtn.on("click", ()=>{
            GameController.inst.showHomeView();
        })
    }

    Show(): void {
        GameInfo.inst.level;
        this.mask.active = false;
        this.initRoles();
    }

    initRoles() {
        let roles = PlayerCenter.inst.roles;

        roles.forEach(role => {
            let roleNode: Node = ObjPool.inst.getRoleNode(role);
            let roleScript: RoleBase = roleNode.getComponent(RoleBase);
            roleNode.parent = this.rolesContent;

            roleNode.off(Node.EventType.TOUCH_START);
            roleNode.off(Node.EventType.TOUCH_MOVE);
            roleNode.off(Node.EventType.TOUCH_CANCEL);
            roleNode.off(Node.EventType.TOUCH_END);

            roleNode.on(Node.EventType.TOUCH_MOVE, (e) => {
                if (!this.isTouchMove) {
                    let pos: Vec3 = roleNode.getComponent(UITransform).convertToWorldSpaceAR(v3(0, 0, 0));
                    pos = this.touch.parent.getComponent(UITransform).convertToNodeSpaceAR(pos);
                    this.touch.position = pos;

                    roleNode.parent = this.touch;
                    roleNode.position = v3(0, 0, 0);

                    this.mask.active = true;
                    this.isTouchMove = true;
                }

                // 获取触摸事件的世界坐标
                let world_pos = e.getUILocation();

                // 将世界坐标转换为节点的本地坐标
                let uiTransform = this.node.getComponent(UITransform);
                let local_pos = uiTransform.convertToNodeSpaceAR(new Vec3(world_pos.x, world_pos.y, 0));

                if (
                    Math.abs(local_pos.x) < this.mapNode.getComponent(UITransform).width / 2
                    && local_pos.y > -this.mapNode.getComponent(UITransform).height / 2
                    && local_pos.y < -GlobalContext.TileSize/2
                ) {
                    const gridSize = GlobalContext.TileSize;
                    const skew = GlobalContext.skew;
                    local_pos.x = Math.round((local_pos.x + skew.x) / gridSize) * gridSize - skew.x;
                    local_pos.y = Math.round((local_pos.y + skew.y) / gridSize) * gridSize - skew.y;

                    this.touch_bg.children[0].active = true;
                    this.touch_bg.children[1].active = false;
                } else {
                    this.touch_bg.children[0].active = false;
                    this.touch_bg.children[1].active = false;
                }
                // 更新 touch 节点的位置
                this.touch.position = local_pos;
            });
            roleNode.on(Node.EventType.TOUCH_CANCEL, () => {
                if (
                    Math.abs(this.touch.position.x) < this.mapNode.getComponent(UITransform).width / 2
                    && this.touch.position.y > -this.mapNode.getComponent(UITransform).height / 2
                    && this.touch.position.y < -GlobalContext.TileSize/2
                ) {
                    let pos: Vec3 = roleNode.getComponent(UITransform).convertToWorldSpaceAR(v3(0, 0, 0));
                    pos = this.battleScene.getComponent(UITransform).convertToNodeSpaceAR(pos);
                    roleNode.parent = this.battleScene;
                    roleScript.pos = pos;
                } else {
                    roleNode.parent = this.rolesContent;
                    roleNode.position = v3(0, 0, 0);
                }
                this.touch_bg.children[0].active = false;
                this.touch_bg.children[1].active = false;
                this.isTouchMove = false;
                this.mask.active = false;
            });
            roleNode.on(Node.EventType.TOUCH_END, () => {
                if (!this.isTouchMove) {
                    GameController.inst.showRoleView(roleNode.name)
                    return;
                }

                if (
                    Math.abs(this.touch.position.x) < this.mapNode.getComponent(UITransform).width / 2
                    && this.touch.position.y > -this.mapNode.getComponent(UITransform).height / 2
                    && this.touch.position.y < -GlobalContext.TileSize/2
                ) {
                    let pos: Vec3 = roleNode.getComponent(UITransform).convertToWorldSpaceAR(v3(0, 0, 0));
                    pos = this.battleScene.getComponent(UITransform).convertToNodeSpaceAR(pos);
                    roleNode.parent = this.battleScene;
                    roleScript.pos = pos;
                } else {
                    roleNode.parent = this.rolesContent;
                    roleNode.position = v3(0, 0, 0);
                }
                this.touch_bg.children[0].active = false;
                this.touch_bg.children[1].active = false;
                this.isTouchMove = false;
                this.mask.active = false;
            });
        })
    }
}